package com.wenjunwu.blog.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.github.pagehelper.PageInfo;
import com.wenjunwu.blog.common.JSONResult;
import com.wenjunwu.blog.common.PageTranslate;
import com.wenjunwu.blog.em.OptionEnum;
import com.wenjunwu.blog.entity.BlogComment;
import com.wenjunwu.blog.service.BlogCommentService;

@Controller
public class BlogCommentController {

	@Autowired
	private BlogCommentService blogCommentService;

	@PreAuthorize("hasRole('USER')")
	@RequestMapping(path = "/portal/add")
	public String save(HttpServletRequest request, RedirectAttributes model) {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		BlogComment blogComment = new BlogComment();
		blogComment.setCname(authentication.getName());
		blogComment.setContentId(Long.valueOf(request.getParameter("contentId")));
		blogComment.setComment(request.getParameter("comment"));
		blogComment.setStatus("publish");
		blogCommentService.save(blogComment);
		return "redirect:".concat("/portal/detail/").concat(String.valueOf(blogComment.getContentId()));
	}

	@ResponseBody
	@RequestMapping(path = "/console/comments.json")
	public PageTranslate<BlogComment> getComments(HttpServletRequest request) {
		String temp = request.getParameter("draw") == null ? "1" : request.getParameter("draw");
		String paramPageNum = request.getParameter("start") == null ? "1" : request.getParameter("start");
		Integer pageSize = Integer
				.valueOf(request.getParameter("length") == null ? "10" : request.getParameter("length"));
		int pageNum = (Integer.valueOf(paramPageNum) / pageSize + 1);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", "VALID");
		params.put("pageNum", pageNum);
		params.put("pageSize", pageSize);
		PageInfo<BlogComment> page = blogCommentService.findCommentPage(params);
		PageTranslate<BlogComment> translate = new PageTranslate<BlogComment>();
		List<BlogComment> result = page.getList();
		translate.setData(result);
		translate.setDraw(Long.valueOf(temp));
		translate.setRecordsTotal(page.getTotal());
		translate.setRecordsFiltered(page.getTotal());
		return translate;
	}
	
	@ResponseBody
	@RequestMapping(path = "console/deleteComment/{id}", method = RequestMethod.POST)
	public JSONResult deleteComment(@PathVariable Long id) {
		if ( blogCommentService.deleteComment(id)>0) {
			JSONResult result = new JSONResult();
			result.setResultCode(OptionEnum.OPTION_SUCCESS.getValue());
			result.setResultContent("评论【"+id+"】已成功删除");
			return result;
		} else {
			JSONResult result = new JSONResult();
			result.setResultCode(OptionEnum.OPTION_FAILURE.getValue());
			result.setResultContent("评论【"+id+"】删除失败");
			return result;
		}
	}
}
